<?php
/* SVN FILE: $Id: authentificationable.php 1839 2008-10-22 12:10:13Z francis $ */
class AuthentificationableBehavior extends ModelBehavior {
	var $settings = array();
	var $defaults=array(
		'password' => 'passwd',
		'password_confirm' => 'temppassword',
		'email' => 'email',
		'email_token' => 'email_token',
		'email_token_expires' => 'email_token_expires',
		'email_authenticated' => 'email_authenticated',
		'active' => 'active',
		'termOfService' => 'tos');
/**
 * Enter description here...
 *
 * @param AppModel $model
 * @param unknown_type $config
 */
	function setup(&$model, $config = array()) {
		$config = array_merge($this->defaults, $config);
		$this->settings[$model->alias] = $config;
	}
	function beforeFind(&$model, $query) {
		$config = $this->settings[$model->alias];
		return $query;
	}
	function afterFind(&$model, $results, $primary) {
		$config = $this->settings[$model->alias];
		return $results;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function beforeSave(&$model) {
		$config = $this->settings[$model->alias];
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function afterSave(&$model, $created) {
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function beforeDelete(&$model) {
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function afterDelete(&$model) {
		return true;
	}

/**
 * Callback
 *
 * @param AppModel $model
 */
	function onError(&$model, $error) {

	}
/**
 * Custom validation method to ensure that the two entered passwords match
 *
 * @param string $tmppassword
 * @return boolean
 */
	function confirmPassword(&$model, $password) {
		$config = $this->settings[$model->name];
		if (isset($model->data[$model->name][$config['password']]) &&
			($password[$config['password_confirm']] != "") &&
			($model->data[$model->name][$config['password']] != $password[$config['password_confirm']])) {
			return false;
		}

		return true;
	}
/**
 * Generate token used by user registration system
 *
 * @return string
 */
	function __generateToken() {
		$possible = '0123456789abcdefghijklmnopqrstuvwxyz';
		$token = "";
		$i = 0;

		while ($i < 20) {
			$char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);

			if (!stristr($token, $char)) {
				$token .= $char;
				$i++;
			}
		}
		return $token;
	}
	function __generatePassword($length = 10) {
		srand((double)microtime()*1000000);
		$password = '';
		$vowels = array("a", "e", "i", "o", "u");
		$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
							"cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl");
		for($i = 0; $i < $length; $i++){
			$password .= $cons[mt_rand(0, 31)] . $vowels[mt_rand(0, 4)];
		}

		return substr($password, 0, $length);
	}
	function saveTemp(&$model, $email){
		$config = $this->settings[$model->name];
		$model->id = $model->field($model->primaryKey, array($model->name.'.'.$config['email'] => $email));
		$model->data[$model->name][$config['password_confirm']] = $this->__generatePassword();
		$token = $this->__generateToken();
		$model->data[$model->name][$config['email_token']] = $token;
		$sixtyMins = time() + 43000;
		$model->data[$model->name][$config['email_token_expires']] = date('Y-m-d H:i:s', $sixtyMins);

		if($model->save($model->data, false)){
			return $token;
		}

		return false;
	}
	function validateToken(&$model, $id = null, $reset = false) {
		$config = $this->settings[$model->name];
		$model->recursive = '-1';
		$data = false;
		$match = $model->find(array($model->name.'.'.$config['email_token'] => $id), $model->primaryKey . ',' . $config['email'] . ',' . $config['password_confirm'] . ',' . $config['email_token_expires']);

		if(!empty($match)){
			$expires = strtotime($match[$model->name][$config['email_token_expires']]);

			if($expires > time()){
				$data[$model->name][$model->primaryKey] = $match[$model->name][$model->primaryKey];
				$data[$model->name][$config['email']] = $match[$model->name][$config['email']];
				$data[$model->name][$config['email_authenticated']] = '1';

				if($reset === true) {
					$data[$model->name][$config['password']] = $this->__generatePassword();
					$data[$model->name][$config['password_confirm']] = null;
				}

				$data[$model->name][$config['email_token']] = null;
				$data[$model->name][$config['email_token_expires']] = null;
			}
			return $data;
		} else {
			return $data;
		}
	}
/**
 * Custom validation method to ensure that use has selected the 'verified I have read the terms and conditions' checkbox
 *
 * @param string $tos
 * @return boolean
 */
	function verifyTos(&$model, $tos) {
		$config = $this->settings[$model->name];
		if ($tos[$config['termOfService']] == 1) {
			return true;
		}
		return false;
	}
}
?>